package com.filetransfer.filesTCP.controller;

import com.filetransfer.filesTCP.service.checkLoginService;
import com.filetransfer.filesTCP.service.queryFilesService;
import com.filetransfer.filesTCP.util.ObjectMapperUtil;
import org.apache.coyote.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

/**
 * Description: these message can be deleted
 * Author:sold out
 * Create Date: 2021/11/23 13:04
 * Version: JDK 1.8
 */
@RestController
public class controller {


    /**
     * 实现文件上传
     */
    @RequestMapping ("/fileUpload")
    @ResponseBody
    public String fileUpload(@RequestParam ("fileName") MultipartFile file) {
        if (file.isEmpty()) {
            return "false";
        }
        String fileName = file.getOriginalFilename();
        String[] split = fileName.split("\\.");
        System.out.println("后缀 : " + split[split.length - 1]);
        int size = (int) file.getSize();
        //        System.out.println(fileName + "-->" + size);
        String path = "X:/";
        File dest = new File(path + "/" + fileName);
        if (!dest.getParentFile().exists()) { //判断文件父目录是否存在
            dest.getParentFile().mkdir();
        }
        try {
            file.transferTo(dest); //保存文件
            return "上传成功!";
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "上传失败!";
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "上传失败!";
        }
    }

    /**
     * 用户登录
     */
    @Autowired
    checkLoginService checkLoginService;

    @PostMapping ("/login")
    public void login(String username, String password, HttpServletResponse response, HttpServletRequest request) throws IOException {
        System.out.println("输入的账号:" + username);
        System.out.println("输入的密码:" + password);
        String realPassword = checkLoginService.checkLogin(username);
        if (realPassword.equals(password)) {
            System.out.println("密码正确....");
            response.sendRedirect(request.getContextPath() + "/html/about.html");
        }
    }


    /**
     * 文件浏览
     */
    @Autowired
    queryFilesService queryFilesService;

    @PostMapping ("/queryAllFiles")
    public void queryAllFiles(HttpServletResponse response) throws IOException {
        List list = queryFilesService.queryAllFiles();
        String s = ObjectMapperUtil.handleObjectToString(list);
        // 通过ajax反回给前端
        response.setContentType("application/jsom;charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.write(s);
        writer.flush();
        writer.close();
    }

    /**
     * 文件下载....
     */
    @GetMapping ("/downloadFiles")
    public void downLoadFiles(HttpServletResponse response, HttpServletRequest request, String path) {

    }




}
